const router = require('koa-router')()
const userService = require('../controllers/mySqlConfig')

router.prefix('/users')

// 定义登录的接口
router.post('/userLogin', async (ctx, next) => {
  // 拿到前端给我的参数
  let _username = ctx.request.body.username
  let _userpwd = ctx.request.body.userpwd
  // console.log(_username, _userpwd);
  // 去数据库里面匹配
  await userService
    .userLogin(_username, _userpwd)
    .then(res => {
      console.log(res)
      let r = ''
      if (res.length) {
        r = 'ok'
        let result = {
          id: res[0].id,
          nickname: res[0].nickname,
          usernaem: res[0].username
        }
        ctx.body = {
          code: 200,
          data: result,
          mess: '登陆成功'
        }
      } else {
        r = 'error'
        ctx.body = {
          code: '80004',
          data: r,
          mess: '账号或密码错误'
        }
      }
    })
    .catch(err => {
      ctx.body = {
        code: '80002',
        data: err,
        mess: '账号或密码错误'
      }
    })
})

// 注册接口
router.post('/userRegister', async (ctx, next) => {
  console.log(ctx.request.body);
  let _username = ctx.request.body.username
  let _userpwd = ctx.request.body.userpwd
  let _nickname = ctx.request.body.nickname

  if (!_nickname || !_username || !_userpwd) {

  }

  await userService.findUser(_username).then(async res => {
    console.log(res);
    if (res.length) {
      ctx.body = {
        code: '80003',
        mess: '账号已存在'
      }
    } else {
      await userService.insertUser([_username, _userpwd, _nickname]).then(res => {
        if (res.affectedRows != 0) {
          r = 'ok'
          console.log(res);
          ctx.body = {
            code: '200',
            data: r,
            mess: '注册成功'
          }
        } else {
          r = 'error'
          ctx.body = {
            code: '80001',
            data: r,
            mess: '注册失败'
          }
        }
      })
    }
  })
})


router.post('/findNoteListByType', async (ctx, next) => {
  let note_type = ctx.request.body.note_type
  console.log(note_type);
  await userService.findNoteListByType(note_type).then(res => {
    let r = ""
    if (res.length > 0) {
      ctx.body = {
        code: 200,
        data: res,
        mess: '查询成功'
      }
    } else {
      r = 'error'
      ctx.body = {
        code: 80001,
        data: r,
        mess: "查询失败"
      }
    }
  })

})

router.post('/findNoteDetailById', async (ctx, next) => {
  let id = ctx.request.body.id
  console.log(id);
  await userService.findNoteDetailById(id).then(res => {
    let r = ""
    if (res.length > 0) {
      ctx.body = {
        code: 200,
        data: res,
        mess: '查询成功'
      }
    } else {
      r = 'error'
      ctx.body = {
        code: 80001,
        data: r,
        mess: "查询失败"
      }
    }
  })
})

router.post('/publishNote', async (ctx, next) => {

  let { noteTitle, noteImage, noteType, noteContent, userId, nickname } = ctx.request.body
  let c_time = m_time = new Date()
  await userService.insertNote([c_time, m_time, noteContent, noteImage, noteTitle, noteType, userId, nickname]).then(res => {
    if (res.affectedRows != 0) {
      r = 'ok'
      console.log(res);
      ctx.body = {
        code: '200',
        data: r,
        mess: '发布成功'
      }
    } else {
      r = 'error'
      ctx.body = {
        code: '80001',
        data: r,
        mess: '发布失败'
      }
    }
  })

})

module.exports = router
